/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dbmeta;

import com.hyts.dbmeta.base.AbstractConnectionHandler;
import com.hyts.dbmeta.config.DbPropertiesConfig;
import com.hyts.dbmeta.config.connection.MySQLConnectionConfig;
import com.hyts.dbmeta.enums.DbTypeEnum;
import com.hyts.dbmeta.handler.DBMetadataHandler;
import com.hyts.dbmeta.handler.mysql.MySqlConnectionHandler;
import com.hyts.dbmeta.handler.mysql.MysqlNewConnectionHandler;
import com.hyts.dbmeta.model.DataSourceMetadata;
import com.hyts.dbmeta.model.TableMetadata;
import lombok.Cleanup;
import lombok.SneakyThrows;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

/**
 * @project-name:coffice-toolbox
 * @package-name:com.hyts.dbmeta
 * @author:LiBo/Alex
 * @create-date:2020-04-02 17:28
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class DBMeta {

    /**
     * getTables 连接数据库
     * @param dbTypeEnum
     * @param ip
     * @param database
     * @param userName
     * @param password
     * @return
     */
    @SneakyThrows
    public static List<TableMetadata> getTables(DbTypeEnum dbTypeEnum, String ip, String database, String userName, String password){
        return DBMetadataHandler.builder().build().getTableMetadata(openConnection(dbTypeEnum,ip,database,userName,password));
    }

    /**
     * getTables 连接数据库获取表袁术
     * @param dbTypeEnum
     * @param database
     * @param userName
     * @param password
     * @return
     */
    @SneakyThrows
    public static List<TableMetadata> getTables(DbTypeEnum dbTypeEnum, String database, String userName, String password){
        return getTables(dbTypeEnum,DbPropertiesConfig.DEFAULT_IP_ADDRESS, database,userName,password);
    }

    /**
     * 获取数据源对象
     * @param dbTypeEnum
     * @param ip
     * @param database
     * @param userName
     * @param password
     * @return
     */
    @SneakyThrows
    public static DataSourceMetadata getDataSource(DbTypeEnum dbTypeEnum,String ip,String database, String userName, String password){
        return DBMetadataHandler.builder().build().getDataSourceMetadata(openConnection(dbTypeEnum,ip,database,userName,password));
    }

    /**
     * 获取数据源对象
     * @param dbTypeEnum
     * @param database
     * @param userName
     * @param password
     * @return
     */
    @SneakyThrows
    public static DataSourceMetadata getDataSource(DbTypeEnum dbTypeEnum,String database, String userName, String password){
        return getDataSource(dbTypeEnum,DbPropertiesConfig.DEFAULT_IP_ADDRESS,database,userName,password);
    }

    @SneakyThrows
    private static Connection openConnection(DbTypeEnum dbTypeEnum, String ip, String database, String userName, String password){
            @Cleanup Connection connection = null;
            switch (dbTypeEnum){
                // 判断Mysql数据库老版本
                case MYSQL_OLD:
                    return new MySqlConnectionHandler(new MySQLConnectionConfig())
                            .getMysqlConnection(ip,database,userName,password);
                // 判断Mysql数据库新版本
                case MYSQL_NEW:
                    return new MysqlNewConnectionHandler(new MySQLConnectionConfig())
                            .getMysqlConnection(ip,database,userName,password);
                default:
                    return new MySqlConnectionHandler(new MySQLConnectionConfig())
                            .getMysqlConnection(ip,database,userName,password);

            }
        }


}
